﻿using System.Collections.Generic;

using Model;
using DAO;
using Ext.Net;
using System.IO;
using System.Web;

/**
 * 作者：陈杰
 * QQ  : 710782046
 * Email:ovenjackchain@gmail.com
 * Web :http://www.qidian10.com
 */
namespace BLL
{
    public class rolefunMgr
    {
        rolefunDao rolefunDao = new rolefunDao();

        public bool Add_rolefun(rolefun rolefun)
        {
            return rolefunDao.Add_rolefun(rolefun);
        }
        public rolefun FindById_rolefun(int id)
        {
            return rolefunDao.FindById_rolefun(id);
        }
        public bool Del_rolefun(rolefun rolefun)
        {
            return rolefunDao.Del_rolefun(rolefun);
        }
        public bool Update_rolefun(rolefun rolefun)
        {
            return rolefunDao.Update_rolefun(rolefun);
        }
        public IList<rolefun> FindAll_rolefun()
        {
            return rolefunDao.FindAll_rolefun();
        }
        public IList<rolefun> GetRoleMenu(string roleid)
        {
            return rolefunDao.FindFunByRoleId_rolefun(roleid);
        }
        public bool DelAllRoleFun(string roleid)
        {
            return rolefunDao.DelAllRoleFun(roleid);
        }
        public IList<userfun> FindRoleFun(string roleid)
        {
            return rolefunDao.FindMyMenu(roleid);
        }
        public bool roleFunGive(string funids)
        {
            userfunDao ufundao=new userfunDao();
            string[] funs = funids.Split(',');
            List<userfun> listfun = (List<userfun>)ufundao.FindAll_userfun(" 1=1 ");
            for (int i = 0; i < funs.Length - 1; i++)
            {
                int fatherid=-1;
                int funid=int.Parse(funs[i]);
                while (fatherid != 0)
                {
                    userfun ufun = listfun.Find(delegate(userfun fun) { return fun.funid == funid; });
                    rolefun _rolefun = new rolefun();
                    _rolefun.roleid = int.Parse(funs[funs.Length - 1]);
                    _rolefun.rolep = 0;
                    _rolefun.funid = ufun.funid;
                    rolefunDao.Add_rolefun(_rolefun);
                    funid = ufun.fatherid;
                    fatherid = ufun.fatherid;
                }
            }
            return true;
        }
        public TreeNodeCollection GetMenu(string roleid)
        {
            List<userfun> roleMenu = (List<userfun>)rolefunDao.FindMyMenu(roleid); //得到用户所有功能菜单
            List<userfun> userFatherFun = roleMenu.FindAll(delegate(userfun fun) { return fun.fatherid == 0; }); //父节点
            List<userfun> userChildernFun = roleMenu.FindAll(delegate(userfun fun) { return fun.fatherid > 0; }); ;//子节点
            if(roleid=="-1")
                return CreateFunTree(roleid, userFatherFun, userChildernFun);
            return CreateTree(roleid,userFatherFun, userChildernFun);
        }
        private TreeNodeCollection CreateTree(string roleid,List<userfun> userFatherFun, List<userfun> userChildernFun)
        {
            TreeNodeCollection nodes = new TreeNodeCollection(false);
            foreach (userfun menu in userFatherFun)
            {
                TreeNode node = new TreeNode();
                //核心
                List<userfun> stack = userChildernFun.FindAll(delegate(userfun fun) { return fun.fatherid == menu.funid; });
                if (stack.Count > 0)
                {
                    node.NodeID = menu.funno;
                    node.Text = menu.funname;
                    node.SingleClickExpand = true;
                    node.Nodes.AddRange(CreateTree(roleid,stack, userChildernFun));
                }
                else
                {
                    node.NodeID = menu.funno;
                    node.Text = menu.funname;
                    if (File.Exists(HttpContext.Current.Server.MapPath("/View/images/Icons/" + menu.funno + ".png")))
                        node.IconCls = "icon-" + menu.funno;
                    else
                        node.IconCls = "icon-none";
                    node.Href = "/View/" + menu.funno;
                    node.Leaf = true;
                }
                nodes.Add(node);
            }
            return nodes;
        }
        //角色分配
        private TreeNodeCollection CreateFunTree(string roleid, List<userfun> userFatherFun, List<userfun> userChildernFun)
        {
            TreeNodeCollection nodes = new TreeNodeCollection(false);
            foreach (userfun menu in userFatherFun)
            {
                TreeNode node = new TreeNode();
                //核心
                List<userfun> stack = userChildernFun.FindAll(delegate(userfun fun) { return fun.fatherid == menu.funid; });
                if (stack.Count > 0)
                {
                    node.NodeID = menu.funid.ToString();
                    node.Text = menu.funname;
                    node.SingleClickExpand = true;
                    node.Checked = ThreeStateBool.False;
                    node.Nodes.AddRange(CreateFunTree(roleid, stack, userChildernFun));
                }
                else
                {
                    node.NodeID = menu.funid.ToString();
                    node.Text = menu.funname;
                    if (File.Exists(HttpContext.Current.Server.MapPath("/View/images/Icons/" + menu.funno + ".png")))
                        node.IconCls = "icon-" + menu.funno;
                    else
                        node.IconCls = "icon-none";
                    node.Checked = ThreeStateBool.False;
                    node.Leaf = true;
                }
                nodes.Add(node);
            }
            return nodes;
        }
    }
}
